package com.example.selfservicestation.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.selfservicestation.R
import com.example.selfservicestation.databinding.ListItemTxtLineBinding


class TextlineAdapter : ListAdapter<String, TextlineAdapter.ViewHolder>(LogDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.list_item_txt_line, parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (itemCount > 0 && position < itemCount) {
            holder.bind(getItem(position))
        }
    }

    inner class ViewHolder(
        private val binding: ListItemTxtLineBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(text: String) {
            with(binding) {
                this.text = text
                executePendingBindings()
            }
        }

    }

}

private class LogDiffCallback : DiffUtil.ItemCallback<String>() {

    override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
        return oldItem == newItem
    }

    override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
        return oldItem == newItem
    }

}

